package gov.pbc.nn.kjgl.train.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.TrainingScore;
import gov.pbc.nn.kjgl.entity.TrainingUser;

@Repository
public class TrainUserDao extends AbstractHibernateDao {

	/**
	 * 参培训人员
	 * 
	 * @param page
	 */
	public void getAll(Page<TrainingUser> page, Criteria criteria, String id) {
		DetachedCriteria query = this.buildDetachedCriteria(criteria, TrainingUser.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("busKey", id));
		this.pagingQuery(page, query);
	}

	/**
	 * 删除人员的时候 查询的是 自己部门的人
	 * 
	 * @param page
	 * @param id
	 */
	public void getAllById(Page<TrainingUser> page, String id) {
		DetachedCriteria query = DetachedCriteria.forClass(TrainingUser.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		query.add(Restrictions.eq("busKey", id));
		query.add(Restrictions.eq("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId()));
		this.pagingQuery(page, query);
	}

	/**
	 * 根据 主表的（TrainingUser）保存的username，查询参培训人员份数表
	 * 
	 * @param page
	 */
	public TrainingScore getAllScor(String username,String busKey) {
//		String username=(String) parameter.get("username");
//		String busKey=(String) parameter.get("busKey");
		String hql = "from TrainingScore where invalid=false and usreName=:username and projectId=:busKey";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		map.put("busKey", busKey);
		List<TrainingScore> ls = this.query(hql, map);
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			// TrainingScore trainingScore = new TrainingScore();
			return null;
		}
	}

	/**
	 * 保存外来人员
	 * 
	 * @param list
	 */

	public void saveOther(List<TrainingUser> list, String id) {
		for (TrainingUser trainingUser : list) {
			EntityState s = EntityUtils.getState(trainingUser);
			if (s == EntityState.NEW) {
				trainingUser.setBusKey(id);
				this.save(trainingUser);
			} else if (s == EntityState.MODIFIED) {
				this.update(trainingUser);
			}
		}
	}

	/**
	 * 保存 修改 培训计划人员 分数。
	 * 
	 * @param list
	 */
	public void save(List<TrainingUser> list) {
		for (TrainingUser trainingUser : list) {
			EntityState s = EntityUtils.getState(trainingUser);
			if (s == EntityState.NEW) {
				this.save(trainingUser);
			} else if (s == EntityState.MODIFIED) {
				this.update(trainingUser);
			}
			String cname = trainingUser.getCname();
			String deptId = trainingUser.getDeptId();
			String username = trainingUser.getUsername();
			String deptName = trainingUser.getDeptName();
			String projectId = trainingUser.getBusKey();
			TrainingScore trainingScore = EntityUtils.getValue(trainingUser, "dataSetTrainingScore");
			if (EntityUtils.getState(trainingScore) == EntityState.NEW) {
				trainingScore.setCname(cname);
				trainingScore.setUsreName(username);
				trainingScore.setDeptId(deptId);
				trainingScore.setDeptName(deptName);
				trainingScore.setProjectId(projectId);
				this.save(trainingScore);
			} else if (EntityUtils.getState(trainingScore) == EntityState.MODIFIED) {
				trainingScore.setProjectId(projectId);
				this.update(trainingScore);
			}
		}
	}

	/**
	 * 删除未参与培训计划人员（可批量删除）
	 * 
	 * @param id
	 */
	public void delete(String id) {
		this.delete(id, TrainingUser.class);
	}

	/**
	 * 根据ID 获得 业务表主键
	 * 
	 * @param id
	 * @return
	 */
	public TrainingUser getTrainingUser(String Id) {
		String hql = "from TrainingUser where id=:Id and invalid=false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("Id", Id);
		List<TrainingUser> ls = this.query(hql, map);
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}
	}
}
